using System;
using System.Collections.Generic;
using System.Timers;

namespace RxjhServer
{
	public class 组队Class : IDisposable
	{
		public string 队长Name;

		public Dictionary<int, Players> 组队列表;

		public List<Players> tem2 = new List<Players>();

		public Dictionary<int, 组队顺序类> 组队顺序列表;

		public ThreadSafeDictionary<int, Players> 队员列表;

		public Players 队长;

		public int 组队id;

		public int 道具分配规则;

		public int 当前组队等级;

		public int 当前分配;

		public Players 邀请人;

		public List<Players> tem = new List<Players>();

		public System.Timers.Timer 自动显示;

		public 组队Class(Players 队长_)
		{
			自动显示 = new System.Timers.Timer(3000.0);
			自动显示.Elapsed += 自动显示事件;
			自动显示.AutoReset = true;
			组队列表 = new Dictionary<int, Players>();
			道具分配规则 = 1;
			当前分配 = 0;
			队长 = 队长_;
			当前组队等级 = 队长_.Player_Level;
			组队顺序列表 = new Dictionary<int, 组队顺序类>();
			队员列表 = new ThreadSafeDictionary<int, Players>();
			队员列表.Add(队长_.人物全服ID, 队长_);
			增加队员(队长_.人物全服ID, 队长_);
		}

		public void Dispose1()
		{
			if (组队列表 != null)
			{
				foreach (Players value in 组队列表.Values)
				{
					value.解散组队提示();
					value.组队id = 0;
				}
				组队列表.Clear();
			}
			if (组队顺序列表 != null)
			{
				组队顺序列表.Clear();
			}
			World.W组队.Remove(组队id);
			if (自动显示 != null)
			{
				自动显示.Enabled = false;
				自动显示.Close();
				自动显示.Dispose();
				自动显示 = null;
			}
			邀请人 = null;
			tem = null;
		}

		public void Dispose()
		{
			try
			{
				foreach (Players value in 组队列表.Values)
				{
					value.解散组队提示();
					value.组队id = 0;
					value.组队阶段 = 0;
					if (value.追加状态列表 != null && value.追加状态列表.ContainsKey(900000035))
					{
						追加状态类 追加状态类 = value.追加状态列表[900000035];
						追加状态类.时间结束事件();
					}
				}
				组队列表.Clear();
				组队顺序列表.Clear();
				World.W组队.Remove(组队id);
				if (自动显示 != null)
				{
					自动显示.Enabled = false;
					自动显示.Close();
					自动显示.Dispose();
					自动显示 = null;
				}
				邀请人 = null;
				tem = null;
			}
			catch (Exception ex)
			{
				Form1.WriteLine(1, "组队类 Dispose 出错!" + ex.Message);
			}
			finally
			{
				tem = null;
				组队列表 = null;
				World.W组队.Remove(组队id);
			}
		}

		private void 自动显示事件(object source, ElapsedEventArgs e)
		{
			if (World.jlMsg == 1)
			{
				Form1.WriteLine(1, "组队Class-自动显示事件");
			}
			int num = 0;
			try
			{
				num = 2;
				if (组队列表.Count <= 1)
				{
					num = 3;
					Dispose();
					num = 4;
				}
				else
				{
					foreach (Players value in 组队列表.Values)
					{
						num = 6;
						if (World.allConnectedChars.ContainsKey(value.人物全服ID))
						{
							num = 7;
							value.显示队员();
						}
						else
						{
							num = 9;
							tem.Add(value);
						}
					}
					num = 10;
					foreach (Players item in tem)
					{
						组队列表.Remove(item.人物全服ID);
					}
					if (tem.Count > 0)
					{
						tem.Clear();
					}
					num = 16;
				}
			}
			catch (Exception ex)
			{
				Form1.WriteLine(1, "组队类 自动显示事件 出错!--[断点位置:" + num.ToString() + "]" + ex.Message);
			}
		}

		public void 委任队长(Players 本人, Players 队长类)
		{
			try
			{
				队长 = 队长类;
				foreach (Players value in 组队列表.Values)
				{
					value.委任队长提示(本人, 队长类);
					value.显示队员();
				}
			}
			catch (Exception ex)
			{
				Form1.WriteLine(1, "组队类 委任队长 出错!" + ex.Message);
			}
		}

		public void 加入队员提示(Players 队员)
		{
			try
			{
				foreach (Players value in 组队列表.Values)
				{
					if (队员 != value)
					{
						value.加入组队提示(队员);
						队员.加入组队提示(value);
						string[] array = World.组队经验加成.Split(';');
					}
					value.显示队员();
					value.秘境状态 = 0;
					value.秘境分数 = 0;
				}
				if (组队列表.Count >= 2)
				{
					自动显示.Enabled = true;
				}
			}
			catch (Exception ex)
			{
				Form1.WriteLine(1, "组队类 加入队员提示 出错!" + ex.Message);
			}
		}

		public void 退出(Players 队员)
		{
			int num = 0;
			try
			{
				if (队员 != null)
				{
					num = 1;
					退出队员(队员.人物全服ID);
					num = 2;
					if (队员.追加状态列表.ContainsKey(900000035))
					{
						追加状态类 追加状态类 = 队员.追加状态列表[900000035];
						追加状态类.时间结束事件();
					}
					num = 3;
					tem2.Remove(队员);
					if (组队列表.Count >= 2)
					{
						num = 4;
						if (队长.人物全服ID != 队员.人物全服ID)
						{
							foreach (Players value in 组队列表.Values)
							{
								value.退出组队提示(队员);
								value.显示队员();
							}
						}
						else
						{
							num = 6;
							Players toPlaye = 队员;
							bool flag = true;
							num = 7;
							foreach (Players value2 in 组队列表.Values)
							{
								if (flag)
								{
									num = 8;
									委任队长(队员, value2);
									num = 9;
									value2.委任队长提示(队员, toPlaye);
									队长 = value2;
									num = 10;
									toPlaye = value2;
									num = 11;
									flag = false;
									value2.显示队员();
								}
								num = 12;
								value2.退出组队提示(队员);
								num = 13;
								value2.显示队员();
								num = 14;
							}
						}
					}
					else
					{
						Dispose();
					}
					num = 15;
					队员.本人退出组队提示();
					num = 16;
					队员.组队id = 0;
					num = 17;
					队员.组队阶段 = 0;
					num = 18;
					队员.秘境状态 = 0;
					num = 19;
				}
			}
			catch (Exception ex)
			{
				Form1.WriteLine(1, "组队类 退出 出错![" + 队员.Userid + "]-[" + 队员.UserName + "]-[断点位置:" + num.ToString() + "]" + ex.Message);
			}
		}

		public bool 增加队员(int 人物全服ID, Players play)
		{
			try
			{
				if (组队列表.Count >= 8)
				{
					return false;
				}
				组队列表.Add(人物全服ID, play);
				if (组队顺序列表.Count == 0)
				{
					组队顺序类 组队顺序类 = new 组队顺序类();
					组队顺序类.ID = 0;
					组队顺序类.WorldID = 人物全服ID;
					组队顺序列表.Add(0, 组队顺序类);
					return true;
				}
				for (int i = 0; i < 8; i++)
				{
					if (!组队顺序列表.ContainsKey(i))
					{
						组队顺序类 组队顺序类2 = new 组队顺序类();
						组队顺序类2.ID = i;
						组队顺序类2.WorldID = 人物全服ID;
						组队顺序列表.Add(i, 组队顺序类2);
						return true;
					}
				}
			}
			catch (Exception ex)
			{
				Form1.WriteLine(1, "增加队员 出错!" + ex.Message);
			}
			return false;
		}

		public void 退出队员(int 人物全服ID)
		{
			try
			{
				组队列表.Remove(人物全服ID);
				Dictionary<int, 组队顺序类> dictionary = new Dictionary<int, 组队顺序类>();
				foreach (组队顺序类 value in 组队顺序列表.Values)
				{
					dictionary.Add(value.ID, value);
				}
				foreach (组队顺序类 value2 in dictionary.Values)
				{
					if (value2.WorldID == 人物全服ID)
					{
						组队顺序列表.Remove(value2.ID);
						break;
					}
				}
				if (dictionary.Count > 0)
				{
					dictionary.Clear();
				}
			}
			catch (Exception ex)
			{
				Form1.WriteLine(1, "退出队员 出错!" + ex.Message);
			}
		}

		public Players 得到对应队员(int key)
		{
			try
			{
				if (!组队顺序列表.TryGetValue(key, out 组队顺序类 value))
				{
					return null;
				}
				if (组队列表.TryGetValue(value.WorldID, out Players value2))
				{
					return value2;
				}
			}
			catch (Exception ex)
			{
				Form1.WriteLine(1, "得到对应队员 出错!" + ex.Message);
			}
			return null;
		}
	}
}
